package com.hj.dao;

import com.hj.model.CartProductVo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author haoshen
 * @version 1.0
 */
@Mapper
@Repository
public interface CartDao {
    @Select("SELECT c.id, c.user_id userId, c.product_id productId, c.quantity, p.`name` proName, p.main_image proMainImage, p.price proPrice, p.`status` proStatus, p.stock proStock, c.checked checked " +
            "FROM hj_product p, hj_cart c  WHERE p.id = c.product_id AND user_id = #{id}")
    List<CartProductVo> getCartProduct(Integer id);

    @Insert("INSERT INTO hj_cart ( user_id, product_id, quantity, checked )\n" +
            "VALUES\n" +
            "( #{userId}, #{proId}, #{count}, 1)")
    Boolean addCartPro(Integer userId, Integer proId, Integer count);

    @Select("SELECT * FROM hj_cart WHERE user_id = #{userId} AND product_id = #{proId}")
    CartProductVo selCartPro(Integer userId,Integer proId);

    @Update("UPDATE hj_cart SET quantity = (quantity + #{count}) WHERE user_id = #{userId} AND product_id = #{proId}")
    void addCount(Integer userId,Integer proId,Integer count);

    @Delete("DELETE FROM hj_cart WHERE user_id = #{userId} AND product_id = #{proId}")
    void delCartPro(Integer userId,Integer proId);

    @Update("update hj_cart SET checked = #{checked} WHERE user_id = #{userId} AND product_id = #{proId}")
    void reChecked(Integer userId,Integer proId,Integer checked);
}
